# -*- coding: utf-8 -*-
from part import *
from material import *
from section import *
from assembly import *
from step import *
from interaction import *
from load import *
from mesh import *
from optimization import *
from job import *
from sketch import *
from visualization import *
from connectorBehavior import *

import math

# 胞元几何形状参数
H = 10.0      # 连杆长度
D = 4.0       # 圆直径
R = 0.5 * D   # 圆半径

# 阵列数目
X_N = 5
Y_N = 5

# 阵列间距
X_SPACE = 2 * (R**2 + (0.5*H)**2) ** 0.5
Y_SPACE = 2 * (R**2 + (0.5*H)**2) ** 0.5

def sin(theta):
  return math.sin(theta / 180.0 * math.pi)

def cos(theta):
  return math.cos(theta / 180.0 * math.pi)

# 一点绕某一点旋转一定角度后的坐标
def rotatePoint(point, center, angle):
  x = (point[0] - center[0]) * cos(angle) - (point[1] - center[1]) * sin(angle) + center[0]
  y = (point[0] - center[0]) * sin(angle) + (point[1] - center[1]) * cos(angle) + center[1]
  return (x, y)

# 创建胞元
def createCellUnit(x, y):

  theta = math.atan(0.5*H / R) / math.pi * 180.0
  L = R / cos(theta)

  center = (x, y)

  point_1_1 = (L + x, y)
  point_1_2 = (R * cos(theta) + x, R * sin(theta) + y)

  point_2_1 = rotatePoint(point_1_1, center, 90)
  point_2_2 = rotatePoint(point_1_2, center, 90)

  point_3_1 = rotatePoint(point_1_1, center, 180)
  point_3_2 = rotatePoint(point_1_2, center, 180)

  point_4_1 = rotatePoint(point_1_1, center, 270)
  point_4_2 = rotatePoint(point_1_2, center, 270)

  center_point = (x, y)
  point_1 = (x + R, y)

  mdb.models['Model-1'].sketches['__profile__'].CircleByCenterPerimeter(center=center_point, point1=point_1)

  mdb.models['Model-1'].sketches['__profile__'].Line(point1=point_1_1, point2=point_1_2)
  mdb.models['Model-1'].sketches['__profile__'].Line(point1=point_2_1, point2=point_2_2)
  mdb.models['Model-1'].sketches['__profile__'].Line(point1=point_3_1, point2=point_3_2)
  mdb.models['Model-1'].sketches['__profile__'].Line(point1=point_4_1, point2=point_4_2)
  
# 阵列胞元
def arrayCellUnit():

  for i in range(Y_N):

    for j in range(X_N):

      x = j * X_SPACE
      y = i * Y_SPACE

      createCellUnit(x, y)

mdb.models['Model-1'].ConstrainedSketch(name='__profile__', sheetSize=200.0)

arrayCellUnit()

mdb.models['Model-1'].Part(dimensionality=THREE_D, name='Part-1', type=
  DEFORMABLE_BODY)
mdb.models['Model-1'].parts['Part-1'].BaseWire(sketch=
  mdb.models['Model-1'].sketches['__profile__'])
del mdb.models['Model-1'].sketches['__profile__']

print(" ")

# 打印参数
print("D: %s " % D)
print("H: %s " % H)
print("X_N: %d " % X_N)
print("Y_N: %d " % Y_N)

# Save by cgp on 2021_06_01-17.37.10; build 6.14-5 2015_08_18-22.37.49 135153
